// pages/visitor/apply/apply.js
var api = require('../../../utils/httpUtil.js');
var dateUtils = require('../../../utils/dateUtils.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index: 1,
    picker: ['商务洽谈', '参加会议', '培训'],
    imgList: [],
    staffName: '',
    staffPhone: '',
    visitorName: '',
    visitorPhone: '',
    visitorCompany: '',
    visitingTime: '',
    remark: '参加会议',
    openId: app.globalData.openId,
    showModal: false,
    acceptRule: false,
    btnDis: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      startDate: dateUtils.currentDate(),
      endDate: dateUtils.currentDate()
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  checkboxChange() {
    this.setData({
      acceptRule: !this.data.acceptRule
    })
  },
  showRule() {
    this.setData({
      showModal: true,
    })
  },
  btnAccept() {
    this.setData({
      showModal: false,
      acceptRule: true
    })
  },
  /**
   * 重置
   */
  resetClick() {
    this.setData({
      staffName: '',
      staffPhone: '',
      visitorName: '',
      visitorPhone: '',
      visitorCompany: ''
    });
  },
  /**
   * 提交
   */
  purposeChange(e) {
    this.setData({
      index: e.detail.value,
      remark: this.data.picker[e.detail.value]
    })
  },
  staffNameInput: function (e) {
    this.setData({
      staffName: e.detail.value
    })
  },
  staffPhoneInput: function (e) {
    this.setData({
      staffPhone: e.detail.value
    })
  },
  visitorNameInput: function (e) {
    this.setData({
      visitorName: e.detail.value
    })
  },
  visitorPhoneInput: function (e) {
    this.setData({
      visitorPhone: e.detail.value
    })
  },
  visitorCompanyInput: function (e) {
    this.setData({
      visitorCompany: e.detail.value
    })
  },

  startDateChange(e) {
    this.setData({
      startDate: e.detail.value
    })
  },
  endDateChange(e) {
    this.setData({
      endDate: e.detail.value
    })
  },
  submitClick() {

    //表单校验
    if (!this.checkForm()) {
      return;
    }

    api.httpRequest(api.visitorApplyAPI, {
        staffName: this.data.staffName,
        staffPhone: this.data.staffPhone,
        visitorName: this.data.visitorName,
        visitorPhone: this.data.visitorPhone,
        visitorCompany: this.data.visitorCompany,
        startDate: this.data.startDate,
        endDate: this.data.endDate,
        remark: this.data.remark,
        openId: this.data.openId,
        hyperlink: '/pages/visitor/approve/approve'
      })
      .then(res => {
        wx.showToast({
          title: res.msg,
          duration: 2000,
          mask: true,
          icon: 'none',
          success: function () {
            if (res.code == 200) {
              setTimeout(function () {
                wx.navigateBack();
              }, 1000);
            }
          }
        })
      })
      .catch(error => {
        wx.showToast({
          title: error.message,
          duration: 1000,
          mask: true,
          icon: 'none'
        })
      });
  },
  checkForm() {
    if (!this.data.acceptRule) {
      wx.showToast({
        title: '请先阅读并同意用户协议',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    // if (!this.data.staffName) {
    //   wx.showToast({
    //     title: '员工姓名不能为空',
    //     icon: 'none',
    //     duration: 2000
    //   });
    //   return false;
    // }

    if (!this.data.staffPhone) {
      wx.showToast({
        title: '员工手机号不能为空',
        icon: 'none',
        duration: 2000
      });
      return false;
    }

    if (!this.data.visitorName) {
      wx.showToast({
        title: '访客姓名不能为空',
        icon: 'none',
        duration: 2000
      });
      return false;
    }

    if (!this.data.visitorPhone) {
      wx.showToast({
        title: '访客手机号不能为空',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    var daysDiff = dateUtils.daysDiff(this.data.startDate, this.data.endDate);
    if (daysDiff > 3) {
      wx.showToast({
        title: '访问时间不能大于3天',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    if (this.data.startDate > this.data.endDate) {
      wx.showToast({
        title: '开始日期不能大于结束日期',
        icon: 'none',
        duration: 2000
      });
      return false;
    }

    // if (!this.data.visitorCompany) {
    //   wx.showToast({
    //     title: '访客公司不能为空',
    //     icon: 'none',
    //     duration: 2000
    //   });
    //   return false;
    // }
    return true;
  }
})